package com.shizhanzhe.zxbapp.my;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.TaskBean;
import com.shizhanzhe.zxbapp.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YJ on 2018/10/23.
 */
public class TaskRecordAdapter extends RecyclerView.Adapter<TaskRecordAdapter.ViewHolder> {
    private Context mContext;
    private List<TaskBean.DataBeanX.DataBean> mData = new ArrayList<>();

    public TaskRecordAdapter(Context context) {
        mContext = context;
    }

    public void updateData(int page, List<TaskBean.DataBeanX.DataBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            if (page == 1){
                mData = dataSet;
            } else {
                mData.addAll(dataSet);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_task_record, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TaskBean.DataBeanX.DataBean dataBean = mData.get(position);
        holder.tvContent.setText(dataBean.getCscon());
        holder.tvTime.setText(Util.stampToDate(dataBean.getCsaddtime()+""));
        if (dataBean.getType()==0){
            holder.tvMoney.setText("－" + dataBean.getAcount()+"元");
            holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.color_green));
        }else {
            holder.tvMoney.setText("+" + dataBean.getAcount()+"元");
            holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.red));
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvContent;
        TextView tvTime;
        TextView tvMoney;
        public ViewHolder(View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvMoney = itemView.findViewById(R.id.tv_money);
        }
    }
}
